package com.sgb.jicai.view.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import com.google.android.material.tabs.TabLayout;
import com.sgb.jicai.R;
import com.sgb.jicai.databinding.ActivityJicaiDatingBinding;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.sgb.jicai.view.fragment.JiCaiChangjiaSecondFragment;
import com.sgb.jicai.view.fragment.JiCaiXuFangSecondFragment;
import com.sgb.link.entity.EventConstant;
import com.swgk.core.base.AppActivity;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;

/**
 * 集采大厅
 * 新增的集采首页搜索结果页面
 */
public class JiCaiDaTingActivity extends AppActivity {
    public final static String URL = "url";
    ActivityJicaiDatingBinding mBind;
    private SlidingFragmentPagerAdapter mPagerAdapter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private String[] mTitles = new String[3];
    private String searchWords;


    /**
     * 集采大厅\集采搜索 共用界面
     *
     * @param context
     */
    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, JiCaiDaTingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        mBind = DataBindingUtil.setContentView(JiCaiDaTingActivity.this, R.layout.activity_jicai_dating);
        mBind.ivBack.setOnClickListener(this::onMultiClick);
        mBind.tvSearch.setOnClickListener(this::onMultiClick);
    }

    @Override
    protected void init() {
        super.init();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initViewPager();
    }

    private void initViewPager() {
        mTitles[0] = "需方发起";
        mTitles[1] = "供方发起";
        mTitles[2] = "企业专项";
        mFragments.add(JiCaiXuFangSecondFragment.newInstance("1","1",mBind.editSearch.getText().toString().trim()));//需方发起
        mFragments.add(JiCaiChangjiaSecondFragment.newInstance(false,mBind.editSearch.getText().toString().trim())); //供方发起
        mFragments.add(JiCaiXuFangSecondFragment.newInstance("2","1",mBind.editSearch.getText().toString().trim()));//企业专项
        mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        mBind.viewPager.setAdapter(mPagerAdapter);
        mBind.viewPager.setOffscreenPageLimit(mTitles.length);
        mBind.tabLayoutTop.setupWithViewPager(mBind.viewPager);
        mBind.tabLayoutTop.setTabMode(TabLayout.MODE_FIXED);
        for (int i = 0; i < mBind.tabLayoutTop.getTabCount(); i++) {
            TabLayout.Tab tab = mBind.tabLayoutTop.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        mBind.tabLayoutTop.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                updateTabTextView(tab, true);
                updata();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                updateTabTextView(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        updateTabTextView(mBind.tabLayoutTop.getTabAt(mBind.tabLayoutTop.getSelectedTabPosition()), true);
    }

    private void updata() {
        Fragment fragment = mFragments.get(mBind.viewPager.getCurrentItem());
        if (null != fragment && fragment instanceof JiCaiXuFangSecondFragment) {
            ((JiCaiXuFangSecondFragment) fragment).getData();
        }
        if (null != fragment && fragment instanceof JiCaiChangjiaSecondFragment) {
            ((JiCaiChangjiaSecondFragment) fragment).getData();
        }
    }


    @Override
    public void onMultiClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.tv_search) {// searchWords = mBind.editSearch.getText().toString().trim();
            //把搜索词发送到子fragment中刷新列表
            EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_HOME_FOUR_TYPE_SEARCH, mBind.editSearch.getText().toString().trim()));
        }
    }

    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.tab_item_textview, null);
        TextView textView = (TextView) view.findViewById(R.id.tv_tab_item_textview);
        textView.setTextColor(getResources().getColor(R.color.color_main_bg));
        textView.setTextSize(15);
        textView.setText(mTitles[currentPosition]);
        return view;
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (tab == null || tab.getCustomView() == null) return;
        if (isSelect) {
            //选中加粗
            TextView tabSelect = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tabSelect.setTextColor(getResources().getColor(R.color.white));
            tabSelect.setTextSize(18);
            tabSelect.setText(tab.getText());
        } else {
            TextView tabUnSelect = (TextView) tab.getCustomView().findViewById(R.id.tv_tab_item_textview);
            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setTextColor(getResources().getColor(R.color.color_main_bg));
            tabUnSelect.setTextSize(15);
            tabUnSelect.setText(tab.getText());
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(JiCaiEventEntity event) {
        if (event.getCode() == EventConstant.EVENT_JICAI_TOP_REFRESH_VISIBLE) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public String getKeyWords() {
        String key = mBind.editSearch.getText().toString().trim();
        return TextUtils.isEmpty(key) ? "" : key;
    }
}
